package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.dao.OrderDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.DateUtils;
import com.itheima.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService{

    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private OrderSettingService orderSettingService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ValidateCodeService validateCodeService;

    @Override
    public Result submitOrder(Map<String, Object> map) throws Exception {
        String validateCode = (String) map.get("validateCode");
        String telephone = (String) map.get("telephone");
//        String redisCode = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        boolean result=validateCodeService.checkCode(telephone + RedisMessageConstant.SENDTYPE_ORDER,validateCode);
        //校验验证码是否正确
        if(result){
//        if(validateCode==null&&redisCode.equalsIgnoreCase(validateCode)){
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }

        //查看预约的日期是否设置预约
        String orderDate = (String) map.get("orderDate");
        OrderSetting orderSetting=null;
        Date date = DateUtils.parseString2Date(orderDate);
        orderSetting=orderSettingService.findOrderSettingByDate(date);
        if(orderSetting==null){
            return new Result(false,MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //查看预约当天是否以预约满了
        if(orderSetting.getNumber()<=orderSetting.getReservations()){
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        //通过手机号查看是注册会员，判断是否重复预约
        Member member=memberService.getMemberByPhone(telephone);
        if(member!=null){
            //注册过，id、套餐类型、日期去查询是否存在重复注册
            Order orderOld = new Order();
            orderOld.setMemberId(member.getId());
            orderOld.setSetmealId(Integer.parseInt((String)map.get("setmealId")));
            orderOld.setOrderDate(date);
            List<Order> orderList=this.getOrder(orderOld);
            if (orderList!=null&&orderList.size()>0){
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }else {
            //没有注册会员，先给注册会员
            member=new Member();
            member.setName((String)map.get("name"));
            member.setSex((String)map.get("sex"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String)map.get("idCard"));
            member.setRegTime(new Date());
            memberService.regMember(member);
        }

        //完全预约信息的填写
        // id  member_id  orderDate   orderType     orderStatus  setmeal_id
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(date);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(Integer.parseInt((String)map.get("setmealId")));
        addOrder(order);
        //发送预约成功的短信并返回预约完成后的预约信息id
//        SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone,"666");
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findOrderById(Integer id) throws Exception {
        Map map=orderDao.findOrderById(id);
        Date date = (Date) map.get("orderDate");
        String orderDate = DateUtils.parseDate2String(date);
        map.put("orderDate",orderDate);
        return map;
    }

    @Override
    public Integer getTodayOrderNumber(String today) {
        Integer count=orderDao.getTodayOrderNumber(today);
        return count;
    }

    @Override
    public Integer getTodayVisitsNumber(String today) {
        Integer count=orderDao.getTodayVisitsNumber(today);
        return count;
    }

    @Override
    public Integer getOrderNumberAfterDate(String date) {
        Integer count=orderDao.getOrderNumberAfterDate(date);
        return count;
    }

    @Override
    public Integer getVisitsNumberAfterDate(String date) {
        Integer count=orderDao.getVisitsNumberAfterDate(date);
        return count;
    }


    public List<Order> getOrder(Order orderOld){
        List<Order> orderList=orderDao.getOrder(orderOld);
        return orderList;
    }

    public void addOrder(Order order){
        orderDao.addOrder(order);
    }
}
